package com.sansui.blog.controller.admin.page;

import com.sansui.blog.pojo.User;
import com.sansui.blog.pojo.UserInfo;
import com.sansui.blog.pojo.vo.UpdatePassword;
import com.sansui.blog.service.UserInfoService;
import com.sansui.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @name: UserController
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-04-17 13:02
 **/
@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("page/users")
    public String adminUsers(){
        return "admin/page/users";
    }

    @PostMapping("page/getUserList")
    @ResponseBody
    public Map<String, Object> getUserList(Integer page, Integer limit){
        Map<String, Object> map = userService.getUserList(page, limit);
        return map;
    }

    @PostMapping("page/user/updateStatus")
    @ResponseBody
    public Map<String, Object> updateUserStatus(Long id , String status){
        Map<String, Object> map = userService.updateUserStatus(id, status);
        return map;
    }


    @GetMapping("page/user/user-setting")
    public ModelAndView userSetting(ModelAndView mv,HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        UserInfo userInfo = userInfoService.getUserInfoByUserId(String.valueOf(data.get("uid")));
        userInfo.setUid(null);
        userInfo.setCreateTime(null);
        userInfo.setRegisterIp(null);
        userInfo.setUpdateTime(null);
        userInfo.setStatus(null);
        mv.addObject("userInfo",userInfo);
        mv.setViewName("admin/page/user/user-setting");
        return mv;
    }

    @PostMapping("page/user/user-setting")
    @ResponseBody
    public Map<String, Object> saveUserInfo(@RequestBody UserInfo userInfo,HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = userInfoService.saveUserInfo(userInfo,String.valueOf(data.get("uid")));
        return map;
    }

    @GetMapping("page/user/user-password")
    public String userPassword(){
        return "admin/page/user/user-password";
    }

    @PostMapping("page/user/updatePassword")
    @ResponseBody
    public Map<String, Object> updatePassword(@RequestBody UpdatePassword updatePassword, HttpSession session){
//        User user = (User) session.getAttribute("user");
        Map<String,Object> data = (Map<String, Object>) session.getAttribute("user");
        Map<String, Object> map = userService.updatePassword(updatePassword,String.valueOf(data.get("uid")));
        return map;
    }


}
